package com.xuesheng.controller.sys;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xuesheng.entity.Jiaoshi;
import com.xuesheng.entity.Student;
import com.xuesheng.entity.SysUser;
import com.xuesheng.entity.UserRole;
import com.xuesheng.entity.vo.JiaoshiVo;
import com.xuesheng.entity.vo.StudentVo;
import com.xuesheng.service.IJiaoshiService;
import com.xuesheng.service.IStudentService;
import com.xuesheng.service.ISysUserService;
import com.xuesheng.service.IUserRoleService;
import com.xuesheng.util.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 登录控制层
 * 2020-07-08
 * author lbk
 */
@Controller
@RequestMapping("/api/login")
public class LoginController{

    private static final Logger log =  LoggerFactory.getLogger(LoginController.class);

    @Autowired
    private ISysUserService iSysUserService;

    @Autowired
    private IUserRoleService iUserRoleService;

    @Autowired
    private IStudentService iStudentService;

    @Autowired
    private IJiaoshiService iJiaoshiService;



    /**
     * 系统PC平台用户登陆
     * @param request
     * @param response
     * @return
     */
    @CrossOrigin
    @RequestMapping(value="/login")
    @ResponseBody
    public Result login(HttpServletRequest request, HttpServletResponse response){
        String account = request.getParameter("account");
        if(StringUtils.isEmpty(account)){
            return Result.reError("账号不能为空");
        }
        String pwd =  request.getParameter("pwd");
        if(StringUtils.isEmpty(pwd)){
            return Result.reError("密码不能为空");
        }
        String type=request.getParameter("type");
        //进行登录认证
        QueryWrapper<SysUser> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("account",account);
        queryWrapper.eq("pwd",pwd);
        queryWrapper.eq("type",type);
        SysUser hjManager=this.iSysUserService.getOne(queryWrapper);
        if(hjManager==null){
            return Result.reError("账号密码错误！！！");
        }
        request.getSession().setAttribute("user",hjManager);
        return Result.reOk("登录成功");
    }

    /**
     * 注册
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value="/register")
    @ResponseBody
    public Result register(HttpServletRequest request, HttpServletResponse response) throws ParseException {
        String type=request.getParameter("type");
        String account=request.getParameter("account");
        String name=request.getParameter("name");
        String pwd=request.getParameter("pwd");
        //验证当前账号是否存在
        QueryWrapper<SysUser> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("account",account);
        queryWrapper.eq("type",type);
        if(this.iSysUserService.getOne(queryWrapper)!=null){
            return Result.reError("账号已存在，请重新更换账号");
        }
        SysUser sysUser=new SysUser();
        sysUser.setAccount(account);
        sysUser.setPwd(pwd);
        sysUser.setCtime(new Date());
        sysUser.setType(type);
        this.iSysUserService.save(sysUser);
        if(type.equals("1")){//管理员注册
            UserRole userRole=new UserRole();
            userRole.setRoleid(1);
            userRole.setUserid(sysUser.getUserid());
            this.iUserRoleService.save(userRole);
            return Result.reOk("注册成功");
        }
        if(type.equals("2")){//教师注册
            Jiaoshi dto=new Jiaoshi();
            dto.setUserid(sysUser.getUserid());
            dto.setName(request.getParameter("name"));
            dto.setSex(request.getParameter("sex"));
            this.iJiaoshiService.save(dto);
            UserRole userRole=new UserRole();
            userRole.setRoleid(2);
            userRole.setUserid(sysUser.getUserid());
            this.iUserRoleService.save(userRole);
            return Result.reOk("注册成功");
        }
        if(type.equals("3")){//学生注册
            Student dto=new Student();
            dto.setUserid(sysUser.getUserid());
            dto.setName(request.getParameter("name"));
            dto.setSex(request.getParameter("sex"));
            this.iStudentService.save(dto);
            UserRole userRole=new UserRole();
            userRole.setRoleid(3);
            userRole.setUserid(sysUser.getUserid());
            this.iUserRoleService.save(userRole);
            return Result.reOk("注册成功");
        }
        return Result.reError("获取注册用户信息异常");
    }


    /**
     * 获取当前登录的用户信息
     * @return
     */
    @RequestMapping("/getUserInfo")
    @ResponseBody
    public Result getUserInfo(HttpSession session) {
        SysUser sysUser=(SysUser) session.getAttribute("user");
        if(sysUser.getType().equals("1")){
            return Result.reOk("查询成功",sysUser);
        }
        if(sysUser.getType().equals("2")){//教师
            Jiaoshi dto=this.iJiaoshiService.getById(sysUser.getUserid());
            JiaoshiVo vo=new JiaoshiVo();
            BeanUtils.copyProperties(dto,vo);
            vo.setAccount(sysUser.getAccount());
            vo.setPwd(sysUser.getPwd());
            vo.setType(sysUser.getType());
            return Result.reOk("查询成功",vo);
        }
        if(sysUser.getType().equals("3")){//学生
            Student dto=this.iStudentService.getById(sysUser.getUserid());
            StudentVo vo=new StudentVo();
            BeanUtils.copyProperties(dto,vo);
            vo.setAccount(sysUser.getAccount());
            vo.setPwd(sysUser.getPwd());
            vo.setType(sysUser.getType());
            return Result.reOk("查询成功",vo);
        }
        return Result.reError("获取当前用户信息异常");
    }


    /**
     * 修改当前登录的用户信息
     * @return
     */
    @RequestMapping("/updateUserInfo")
    @ResponseBody
    public Result updateUserInfo(HttpSession session,StudentVo studentVo,JiaoshiVo jiaoshiVo) {
        SysUser sysUser=(SysUser) session.getAttribute("user");
        if(sysUser.getType().equals("1")){//管理员
            return Result.reOk("修改成功",sysUser);
        }
        if(sysUser.getType().equals("2")){//教师
            Jiaoshi dto=this.iJiaoshiService.getById(sysUser.getUserid());
            BeanUtils.copyProperties(jiaoshiVo,dto,new String[]{"userid"});
            this.iJiaoshiService.updateById(dto);
            sysUser.setPwd(jiaoshiVo.getPwd());
            this.iSysUserService.updateById(sysUser);
            return Result.reOk("修改成功");
        }

        if(sysUser.getType().equals("3")){//学生
            Student dto=this.iStudentService.getById(sysUser.getUserid());
            BeanUtils.copyProperties(studentVo,dto,new String[]{"userid"});
            this.iStudentService.updateById(dto);
            sysUser.setPwd(studentVo.getPwd());
            this.iSysUserService.updateById(sysUser);
            return Result.reOk("修改成功");
        }
        return Result.reError("修改用户信息异常");
    }


    /**
     * 修改密码
     * @return
     */
    @RequestMapping("/updatePassWord")
    @ResponseBody
    public Result updatePassWord(HttpSession session,HttpServletRequest request) {
        SysUser sysUser=(SysUser) session.getAttribute("user");
        if(!request.getParameter("pwd").equals(sysUser.getPwd())){
            return Result.reError("密码校验失败");
        }
        if(!request.getParameter("newPwd").equals(request.getParameter("aginPwd"))){
            return Result.reError("两次密码输入不一致");
        }
        sysUser.setPwd(request.getParameter("newPwd"));
        this.iSysUserService.updateById(sysUser);
        return Result.reOk("修改成功");
    }

    /**
     * 路由地址
     */
    public String prefix = "/view/admin/account";

    /**
     * 进入管理页面
     * @return
     */
    @RequestMapping("/index")
    public String index(){
        return prefix+"/index";
    }


}
